20.2 Automatic Restart(自动重启)
1 | Applications that use spring-boot-devtools automatically restart |
如果类路径下的文件被改动了,您的应用将会使用spring-boot-devtools自动重启。这将会是一个作用于您的IDE的非常有用的功能,它将会以最快的速度给您的代码修改做出反馈。默认情况下,任何类路径下的东西和文件夹中的东西都将会被监控是否被修改。注意某些资源(如静态资产和视图模板)不需要重新启动应用程序。
1 | Triggering a restart |
触发重启
由于DevTools监控类路径下的资源,所以只有更新类路径资源才能触发重启。如何触发类路径资源变更从而触发重启取决于您使用的IDE。在Eclipse中,保存修改文件将会触发重启,在IDEA中,构建项目会产生同样的效果。(Build -> Build Project)1
2
3
4
5As long as forking is enabled, you can also start your
application by using the supported build plugins (Maven
and Gradle), since DevTools needs an isolated application
classloader to operate properly. By default, Gradle and Maven
do that when they detect DevTools on the classpath.
只要使用分叉,您可以使用插件(Maven和Gradle)启动您的应用,因为DevTools需要隔离应用类加载器去操作属性。默认情况下,当他们在类路径中检测到DevTools时,Gradle和Maven会这样做。1
2
3
4
5Automatic restart works very well when used with LiveReload.
See the LiveReload section for details. If you use JRebel,
automatic restarts are disabled in favor of dynamic class
reloading. Other devtools features (such as LiveReload and
property overrides) can still be used.
与LiveReload一起使用时,自动重启的效果非常好。可以查看20.3-LiveReload章节了解更多细节。如果你使用JRebel,自动重新启动被禁用,以支持动态类重装。 其他devtools功能(如LiveReload和属性覆盖)仍然可以使用。1
2
3DevTools relies on the application context’s shutdown hook to
close it during a restart. It does not work correctly if you have
disabled the shutdown hook (SpringApplication.setRegisterShutdownHook(false)).
重启的时候,DevTools依赖应用上下文的关闭钩子去关闭它。如果您禁用了关闭钩子(SpringApplication.setRegisterShutdownHook(false)),它将无法正常工作。1
2
3
4When deciding if an entry on the classpath should trigger a
restart when it changes, DevTools automatically ignores
projects named spring-boot,spring-boot-devtools, spring-boot-
autoconfigure, spring-boot-actuator, and spring-boot-starter.
当决策类路径下某个条目改变了是否应该触发重启的时候,DevTools自动忽略那些以spring-boot,spring-boot-devtools, spring-boot-autoconfigure, spring-boot-actuator, and spring-boot-starter命名的项目名称。1
2
3
4DevTools needs to customize the ResourceLoader used by the
ApplicationContext. If your application provides one already,
it is going to be wrapped. Direct override of the getResource
method on the ApplicationContext is not supported.
DevTools需要自定义ApplicationContext中使用的资源加载器(ResourceLoader)。如果您的应用已经提供了一个,他将被代理包裹起来。不允许直接重写ApplicationContext中的getResource方法。1
2
3
4
5
6
7
8
9
10
11
12
13Restart vs Reload
The restart technology provided by Spring Boot works by using
two classloaders. Classes that do not change (for example, those
from third-party jars) are loaded into a base classloader. Classes
that you are actively developing are loaded into a restart classloader.
When the application is restarted, the restart classloader is thrown
away and a new one is created. This approach means that application
restarts are typically much faster than “cold starts”, since the base
classloader is already available and populated.
If you find that restarts are not quick enough for your applications
or you encounter classloading issues, you could consider reloading
technologies such as JRebelfrom ZeroTurnaround. These work by rewriting
classes as they are loaded to make them more amenable to reloading.
重启和重载
Spring Boot是通过使用2个类加载器来提供重启技术的。不被修改得类(例如第三方jar)将会被加载到一个基础的类加载器中。那些您真实要修改的类将会被加载到一个“重启”类加载器中。当应用重启的时候,“重启”类加载器将会被抛弃并重新创建一个新的“重启”类加载器。这种方法意味着应用程序重新启动通常比“冷启动”快得多,因为基类加载器已经可用并且已被填充。
如果您发现重启对于您的应用程序来说不够快,或者遇到类加载问题,则可以考虑从ZeroTurnaround中重新加载技术,例如JRebel。 这些工作通过在加载类时重写类来使它们更易于重新加载。